<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php


class FillBase extends ListBase
{
    
    public function filFillList($list){
        $fileList = Array();
        foreach($list as $file){
            $newFile = $this->fileFill($file);
            array_push($fileList,$newFile);
        }
        return $fileList;
    }
    
    
    public function filSearchFillList($list){
        $fileList = Array();
        foreach($list as $file){
            $newFile = $this->fileSearchFill($file);
            array_push($fileList,$newFile);
        }
        return $fileList;
    }


    
    public function fileFill($file){
        $array = Array();
        $array['id']               = $file['id'];
        $array['file_type']        = $file['file_type'];
        $array['parent_file_id']   = $file['parent_file_id'];
        $array['file_update_time'] = CUtils::getFormatTime($file['file_update_time']);
        $array['file_name']        = $file['file_name'];
        $array['file_size']        = CUtils::getFormatSize($file['file_size']);
        $array['byte']             = $file['file_size'];
        $array['version_id']       = $file['version_id'];
        $path = substr($file['file_path'], 1);
        $index = strpos($path, "/");
        $filePath = substr($path, $index);
        $array['file_path']        = $filePath;
        $array['created_at']       = CUtils::getFormatTime(strtotime($file['created_at']));
        $array['updated_at']       = CUtils::getFormatTime(strtotime($file['updated_at']));
        $filePath                  = CUtils::pathinfo_utf($file['file_name']);
        $array['ext']              = strtolower($filePath["extension"]);
                $map_enable=Yii::app()->params['app']['gpsEnable'];
                                if($map_enable && $file['file_type'] != 1) { 
            
            $gps = new AlbumList();
            $array['gps'] = $gps->submitGps($file['file_path'], $file['version_id']);
        } else {
            $array['gps'] = "";
        }
                $array = $this->isShare($array);
        return $array;
    }
    
    
    public  function fileSearchFill($file){
        $retFile  = $this->fileFill($file);
        $fileType = $this->getFileType($file);
        $retFile["type"] = $fileType;
        return $retFile;
    }
    
    public function isShare($array){
    	if($array['file_type'] == 0){
    		
    	}
        $user = Yii::app()->session["user"];
        $user_id = $user["id"];
        
        $data = FileStar::model()->findByUseridAndFileid($user_id, $array['id']);
        if(!empty($data)){
        	$array['is_star']  = 1;
        	$array['p_id']      = $data[0]['id'];
        }else{
        	$array['is_star']  = 0;
        	$array['p_id']      = -1;
        }
        return $array;
    }
    
    
    public function getFileType($file){
        $typeNum = 1;
        $fileVersion = FileVersion::model()->findByPk($file["version_id"]);
        
        $officeTypes = Yii::app()->params['officeType'];
        if (in_array($fileVersion["mime_type"], $officeTypes)){
            $typeNum = 2;
        }else {
            $miniType    = $fileVersion["mime_type"];
            $type = explode("/",$miniType);
            if ($type[0] == "image"){
                $typeNum = 3;
            }elseif($type[0] == "audio"){
                $typeNum = 4;
            }elseif($type[0] == "video"){
                $typeNum = 9;
            }
        }
        return $typeNum;
    }
    
    
    public function retTreeFill($file){
        $array = Array();
        $array[$file['id']]  = $this->treeFill($file);
        return $array;
    }
    
    
    public function treeFill($file){
        $array = Array();
        $array['pid']                  = $file['parent_file_id'];
        $array['cid']                  = $file['id'];
        $array['name']                 = $file['file_name'];
        $array['aid']                  = "1";
        $array['is_share']             = "0";
        $array['category_file_count']  = CUtils::getFormatTime($file['file_update_time']);
        $array['category_cover']       = "";
        $array['pick_code']            = "0";
        $array['s']                    = "0";
        $filePath = CUtils::pathinfo_utf($file['file_name']);
        $array['ext']                  = strtolower($filePath["extension"]);
        return $array;
    }
}
?>